﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Multivee.UI
{
    internal class ScreenArrayEditorController
    {
        private static ScreenArrayEditor _editorWindow = null;
        private const int DefaultScreenCount = 3;
        private static volatile ScreenArrayEditorController _instance = null;
        private static readonly object _instanceLock = new object();
        private ScreenViewConfiguration[] _screenArray;

        private ScreenArrayEditorController()
        {
        }
        
        public static ScreenArrayEditorController Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_instanceLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new ScreenArrayEditorController();
                        }
                    }
                }

                return _instance;
            }
        }

        public ScreenArrayEditor GetEditorWindow(bool hide)
        {
            if (_editorWindow == null)
            {
                _editorWindow = new ScreenArrayEditor(this, hide);
            }

            return _editorWindow;
        }

        internal void SetScreenArray()
        {
            _screenArray = NewScreenArray();
            Point location = new Point(_editorWindow.Padding.Size.Width, _editorWindow.Padding.Size.Height);
            int i = 0;

            foreach (ScreenViewConfiguration screen in _screenArray)
            {
                _editorWindow.AddScreen(NewScreen(screen.Thumbnail, screen.Name, i++));
            }
        }

        internal void AddScreen()
        {
            if (_screenArray == null)
                throw new NullReferenceException("There is no screen array to add a screen to.");

            _editorWindow.AddScreen(NewScreen(null, "New Screen", _editorWindow.Screens.Count));
        }

        /// <summary>
        /// Creates a new screen edit view control, initiates it's properties and event handlers, and returns the control.
        /// </summary>
        /// <param name="thumbnail">The default thumbnail to be displayed in the control.</param>
        /// <param name="name"></param>
        /// <param name="maxIndex"></param>
        /// <param name="screenIndex"></param>
        /// <returns></returns>
        private ScreenEditView NewScreen(Image thumbnail, string name, int screenIndex)
        {
            ScreenEditView view = new ScreenEditView(new ScreenViewConfiguration() { Name = name, ScreenIndex = screenIndex });
            view.ImageRequested += ImageRequestedByView;
            view.Selected += view_Selected;
            
            if (thumbnail != null)
                view.Mosaic = thumbnail;

            return view;
        }

        private void view_Selected(ScreenEditView screen)
        {
            _editorWindow.PropertyGrid.SelectedObject = screen;
        }

        private static Image GetImage()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            DialogResult result = dialog.ShowDialog();

            if (result != DialogResult.OK)
                return null;

            Bitmap image;

            try
            {
                image = (Bitmap)Bitmap.FromFile(dialog.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

            return image;
        }

        private static ScreenViewConfiguration[] NewScreenArray()
        {
            List<ScreenViewConfiguration> list = new List<ScreenViewConfiguration>(DefaultScreenCount);
            list.Add(new ScreenViewConfiguration() { Name = "Screen 1", ScreenIndex = 0, ScreenSize = Screen.PrimaryScreen.WorkingArea.Size });
            //list.Add(new ScreenViewConfiguration() { Name = "Screen 2", ScreenIndex = 0, Tile = Properties.Resources.Tile2, TileDensity = 8, ScreenSize = Screen.PrimaryScreen.WorkingArea.Size });
            //list.Add(new ScreenViewConfiguration() { Name = "Screen 3", ScreenIndex = 0, Tile = Properties.Resources.Tile3, TileDensity = 4, ScreenSize = Screen.PrimaryScreen.WorkingArea.Size });

            return list.ToArray();
        }

        private Image ImageRequestedByView()
        {
            return GetImage();
        }
    }
}
